/********************************************************************************
 * Create by   : Andy Yang
 * Date        : 06-Sep-2006
 * 
 * Copyright (c) 2004 by PCCW Limited
 *
 * All rights reserved. All information contained in this software is confidential and proprietary to
 * PCCW Limited. No part of this software may be reproduced or transmitted in any form or any means,
 * electronic, mechanical, photocopying, recording or otherwise stored in any retrieval system of any
 * nature without the prior written permission of PCCW Limited.
 *
 * This material is a trade secret and its confidentiality is strictly maintained. Use of any copyright
 * notice does not imply unrestricted public access to this material.
 ********************************************************************************
 */

package com.ejd.common.config;

import java.util.List;


import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.ConfigurationFactory;
import org.apache.log4j.Logger;

public class ConfigManager {
    private static Logger logger = Logger.getLogger(ConfigManager.class);
    private static ConfigManager instance = null;
    private ConfigurationFactory factory = new ConfigurationFactory();
    private  Configuration config = null;
    public static String WHARFAGE_FOR_KOREA_NODE=null;
    public static Boolean VAT_FOR_KOREA_NODE=null;

    private ConfigManager() {

    }

    public boolean load() {
        boolean result = false;
        try {
            factory.setConfigurationURL(getClass().getResource("application-configs.xml"));
            config = factory.getConfiguration();
            // S20081012 Jeff Bug 782
            //System.setProperty("deploy.dir", config.getString("deploy.dir"));
            // E20081012 Jeff Bug 782
            result = true;
        } catch (ConfigurationException e) {

            e.printStackTrace();
        }
        return result;
    }

    public static ConfigManager getInstance() {
        if (instance == null) {
            synchronized (ConfigManager.class) {
                if (instance == null) {
                    instance = new ConfigManager();
                }
            }
        }
        return instance;
    }

    public void reloadConfiguration() {
        try {
            config = factory.getConfiguration();
        } catch (ConfigurationException e) {
            logger.error("Reload configuration fails");
        }
    }


    /**
     * @param key - property name, case insensitive
     * @return java.lang.String - null if property isn't found
     */
    public static String getProperty(String key) {
        if (ConfigManager.getInstance().config == null) {
            ConfigManager.getInstance().load();
        }
//        ConfigManager.getInstance().reloadConfiguration();
        String value = ConfigManager.getInstance().config.getString(key.toUpperCase());
        if (value == null || value.equals("")) {
            value = ConfigManager.getInstance().config.getString(key);
        }
        return value;
    }
     
    /**
     * @param key - property name, case insensitive
     * @return java.lang.String - null if property isn't found
     */
    public static String[] getPropertyList(String key) {
    	if (ConfigManager.getInstance().config==null)
    		ConfigManager.getInstance().load();
    	List list=ConfigManager.getInstance().config.getList(key.toUpperCase());
    	if (list==null||list.size()==0)
    		list=ConfigManager.getInstance().config.getList(key);
    	String[] value=new String[list.size()];  
    	for (int i=0;i<list.size();i++){
    		  value[i]=(String)list.get(i);
    		 
    	  }
    	  	return value;
    }


    

    /**
     * @param keys - properties' names, case insensitive
     * @return java.lang.String[] - null if property isn't found
     */
    public static String[] getProperties(String[] keys) {
       
        if (keys == null) return null;
        int m = keys.length;
        String[] rt = new String[m];
        for (int i = 0; i < m; i++) {
            rt[i] = (String)getProperty(keys[i].toUpperCase());
            //S20090325 Bug3496 Benson So  
            if(rt[i] == null || rt[i].equals("")){
            	rt[i] = (String)getProperty(keys[i]);
            }
            //E20090325 Bug3496 Benson So
        }
        return rt;
    }

}
